package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.accountmanage.entity.entityVo.UserMenuVo;
import com.antdesignbackend.backend.accountmanage.service.UserMenuService;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.entity.entityVo.RoleMenuVo;
import com.antdesignbackend.backend.service.RoleMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.List;

@Controller
public class layoutController extends BaseController {
    @Autowired
    RoleMenuService roleMenuService;

    @Autowired
    UserMenuService userMenuService;

    /**
     * 根据角色id查询菜单，允许一个用户多个角色
     */
    @PostMapping("/menu")
    @ResponseBody
    public ReturnVO menu(@RequestBody RoleMenuVo roleMenuVo) {
        ReturnVO returnVO = new ReturnVO();
        if(roleMenuVo.getRoleIds()==null){
            roleMenuVo.setRoleIds(getRoleId());
        }
        if (getLoginGrade() == null || getLoginGrade() == 1){ //一级账户，主账号
            if (roleMenuVo != null) {
                QueryWrapper<RoleMenuVo> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("rm.RoleID", roleMenuVo.getRoleIds()).eq("activity", 1);
                List<RoleMenuVo> roleMenuVos = roleMenuService.selectRoleMenuList(queryWrapper, null);

                if (roleMenuVos.size() > 0) {
                    if ("3".equals(getUserType())){
                        //如果是猪1，移除产蛋记录，如果是鸡0，移除繁殖记录
                        Iterator iterator = roleMenuVos.iterator();
                        while (iterator.hasNext()){
                            // 获得所有菜单
                            RoleMenuVo roleMenuVo1 = (RoleMenuVo)iterator.next();
                            if (getLoginAnimalTypes() != null){
                                if (getLoginAnimalTypes() == 1){ //猪，牛，羊
                                    if ("egg".equals(roleMenuVo1.getKeyName())){ //移除产蛋记录
                                        iterator.remove();
                                    }
                                }
                                if (getLoginAnimalTypes() == 0){ //养鸡，鸭...
                                    if ("reproductionManage".equals(roleMenuVo1.getKeyName())){ //移除繁殖记录
                                        iterator.remove();
                                    }
                                    if ("monthReportRecord".equals(roleMenuVo1.getKeyName())){ //移除月报管理
                                        iterator.remove();
                                    }
                                    if ("ReservationManage".equals(roleMenuVo1.getKeyName())){ //移除留种记录
                                        iterator.remove();
                                    }
                                    if ("BasicManagement".equals(roleMenuVo1.getKeyName())){ //移除基础设置
                                        iterator.remove();
                                    }
                                    if ("SetDay".equals(roleMenuVo1.getKeyName())){ //移除自动转型天数设置
                                        iterator.remove();
                                    }
                                    if ("SetBirthDay".equals(roleMenuVo1.getKeyName())){ //移除预产期天数设置
                                        iterator.remove();
                                    }
                                }
                            }
                        }
                    }
                    returnVO.setCode("200");
                    returnVO.setData(roleMenuVos);
                } else
                    returnVO.setCode("503");
            } else {
                returnVO.setCode("503");
            }
        }else if (getLoginGrade() == 2){ //二级账户，子账号
            //根据用户id查询到对应的菜单
            QueryWrapper<UserMenuVo> wrapper = new QueryWrapper<>();
            wrapper.eq("um.UserId",getLoginUserId());
            List<UserMenuVo> list = userMenuService.selectUserMenuList(wrapper);
            returnVO.setData(list);
        }
        return returnVO;
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().removeAttribute("LoginName");
        request.getSession().removeAttribute("RoleID");
        request.getSession().removeAttribute("RoleCheckKey");
        request.getSession().removeAttribute("Key");
        return "redirect:/login";
    }
}
